#include <stdlib.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>

#define NIT_SHORT_NAMES
#include <nit/socket.h>

#include "../tp.h"
#include "../tr.h"
#include "../io.h"

void
echo(Nit_joint *in, Nit_joint *out)
{
        T3_trad trad;
	T3_tpad ad = {
		.spc = 0,
		.chunk = 1
	};

	t3_trad_init(&trad, 0);

	while (1) {
		switch (t3_tr(in, &trad)) {
		case NIT_JOIN_NONE:
			break;
		case NIT_JOIN_CLOSED:
		case NIT_JOIN_ERROR:
			t3_trad_release(&trad);
			return;
		case NIT_JOIN_OK:
			ad.bytes = trad.buf;
			ad.size = trad.msg;

			t3_tp(NULL, out, &ad, NULL);

			break;
		}
	}
}

int
main(int argc, char *argv[])
{
	Nit_joint *jnt_out;
	Nit_joint *jnt_in;

	if (!t3_io_both("t3/in", "t3/out", &jnt_in, &jnt_out))
		return 0;

	if (!t3_tr_rate_change(jnt_in, 0))
		return 0;

	echo(jnt_in, jnt_out);
	joint_free(jnt_in);
	joint_free(jnt_out);
}
